{
 "metadata": {
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.5-final"
  },
  "orig_nbformat": 2,
  "kernelspec": {
   "name": "python3",
   "language": "python",
   "display_name": "Python 3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2,
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": "   gender  age  status  city  cost  device  y\n0       0    0       0     1     1       0  1\n1       1    1       1     2     1       0  1\n2       0    1       2     1     0       0  1\n3       1    1       2     1     1       1  0\n4       0    1       2     2     0       1  1",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>gender</th>\n      <th>age</th>\n      <th>status</th>\n      <th>city</th>\n      <th>cost</th>\n      <th>device</th>\n      <th>y</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>1</td>\n      <td>1</td>\n      <td>0</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>1</td>\n      <td>1</td>\n      <td>1</td>\n      <td>2</td>\n      <td>1</td>\n      <td>0</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>0</td>\n      <td>1</td>\n      <td>2</td>\n      <td>1</td>\n      <td>0</td>\n      <td>0</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>1</td>\n      <td>1</td>\n      <td>2</td>\n      <td>1</td>\n      <td>1</td>\n      <td>1</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>0</td>\n      <td>1</td>\n      <td>2</td>\n      <td>2</td>\n      <td>0</td>\n      <td>1</td>\n      <td>1</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 数据加载\n",
    "data = pd.read_csv('data.csv')\n",
    "data.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "    gender  age  status  city  cost  device\n",
      "0        0    0       0     1     1       0\n",
      "1        1    1       1     2     1       0\n",
      "2        0    1       2     1     0       0\n",
      "3        1    1       2     1     1       1\n",
      "4        0    1       2     2     0       1\n",
      "5        0    2       2     2     1       0\n",
      "6        1    0       0     0     0       1\n",
      "7        1    1       0     0     1       1\n",
      "8        0    1       1     0     0       0\n",
      "9        0    1       0     0     0       1\n",
      "10       0    0       0     2     1       0\n"
     ]
    }
   ],
   "source": [
    "# X 赋值\n",
    "X = data.drop(['y'], axis=1)\n",
    "print(X)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0     1\n",
      "1     1\n",
      "2     1\n",
      "3     0\n",
      "4     1\n",
      "5     0\n",
      "6     0\n",
      "7     1\n",
      "8     0\n",
      "9     0\n",
      "10    0\n",
      "Name: y, dtype: int64\n"
     ]
    }
   ],
   "source": [
    "# y 赋值\n",
    "y = data.loc[:, 'y']\n",
    "print(y)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "outputs": [],
   "source": [
    "# 建立模型\n",
    "from sklearn.naive_bayes import CategoricalNB\n",
    "model = CategoricalNB()"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "outputs": [
    {
     "data": {
      "text/plain": "CategoricalNB()"
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 模型训练\n",
    "model.fit(X, y)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[0.48480565 0.51519435]\n",
      " [0.31114639 0.68885361]\n",
      " [0.3341655  0.6658345 ]\n",
      " [0.28647866 0.71352134]\n",
      " [0.5009356  0.4990644 ]\n",
      " [0.58532423 0.41467577]\n",
      " [0.80059811 0.19940189]\n",
      " [0.61627001 0.38372999]\n",
      " [0.60089784 0.39910216]\n",
      " [0.72801439 0.27198561]\n",
      " [0.58532423 0.41467577]]\n"
     ]
    }
   ],
   "source": [
    "y_predict_prob = model.predict_proba(X)\n",
    "print(y_predict_prob)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    },
    "tags": []
   }
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1 1 1 1 0 0 0 0 0 0 0]\n"
     ]
    }
   ],
   "source": [
    "# 输出预测的y\n",
    "y_predict = model.predict(X)\n",
    "print(y_predict)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    },
    "tags": []
   }
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.7272727272727273\n"
     ]
    }
   ],
   "source": [
    "# 计算模型准确率\n",
    "from sklearn.metrics import accuracy_score\n",
    "accuracy = accuracy_score(y, y_predict)\n",
    "print(accuracy)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    },
    "tags": []
   }
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[0 0 0 1 1 0]]\n"
     ]
    }
   ],
   "source": [
    "# 测试样本的预测\n",
    "# gender age status city cost device y\n",
    "# 0      0   0      1    1    0      ?    \n",
    "X_test = np.array([[0, 0, 0, 1, 1, 0]])\n",
    "print(X_test)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    },
    "tags": []
   }
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[0.48480565 0.51519435]]\n"
     ]
    }
   ],
   "source": [
    "y_test_proba = model.predict_proba(X_test)\n",
    "print(y_test_proba)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    },
    "tags": []
   }
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1]\n"
     ]
    }
   ],
   "source": [
    "y_test = model.predict(X_test)\n",
    "print(y_test)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    },
    "tags": []
   }
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1]\n"
     ]
    }
   ],
   "source": [
    "y_test = model.predict(X_test)\n",
    "print(y_test)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": "CategoricalNB()"
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 模型训练\n",
    "model.fit(X, y)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[0.48480565 0.51519435]\n",
      " [0.31114639 0.68885361]\n",
      " [0.3341655  0.6658345 ]\n",
      " [0.28647866 0.71352134]\n",
      " [0.5009356  0.4990644 ]\n",
      " [0.58532423 0.41467577]\n",
      " [0.80059811 0.19940189]\n",
      " [0.61627001 0.38372999]\n",
      " [0.60089784 0.39910216]\n",
      " [0.72801439 0.27198561]\n",
      " [0.58532423 0.41467577]]\n"
     ]
    }
   ],
   "source": [
    "y_predict_prob = model.predict_proba(X)\n",
    "print(y_predict_prob)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1 1 1 1 0 0 0 0 0 0 0]\n"
     ]
    }
   ],
   "source": [
    "# 输出预测的y\n",
    "y_predict = model.predict(X)\n",
    "print(y_predict)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.7272727272727273\n"
     ]
    }
   ],
   "source": [
    "# 计算模型准确率\n",
    "from sklearn.metrics import accuracy_score\n",
    "accuracy = accuracy_score(y, y_predict)\n",
    "print(accuracy)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[0 0 0 1 1 0]]\n"
     ]
    }
   ],
   "source": [
    "# 测试样本的预测\n",
    "# gender age status city cost device y\n",
    "# 0      0   0      1    1    0      ?    \n",
    "X_test = np.array([[0, 0, 0, 1, 1, 0]])\n",
    "print(X_test)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[0.48480565 0.51519435]]\n"
     ]
    }
   ],
   "source": [
    "y_test_proba = model.predict_proba(X_test)\n",
    "print(y_test_proba)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1]\n"
     ]
    }
   ],
   "source": [
    "y_test = model.predict(X_test)\n",
    "print(y_test)"
   ]
  }
 ]
}